/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 *
 * @author Administrator
 */
public class ChooseRoomScene extends Scene {

    public static final int ROW_ITEM_HEIGHT = 50;
    public static final int NUMBER_TOTAL_ROOMS = 10;
    public static final int NUMBER_LOW_LEVEL_ROOMS = 5;
    public static final int NUMBER_MEDIUM_LEVEL_ROOMS = 3;
    //----------------------------------------------------------------------
    private boolean isDrawSelected;
    private boolean enableSlide;
    private int gamePlayType;
    private int startDrawIndex;
    private int roomSelectedIndex;
    private int listSelectedTop;
    private int pointerPressX;
    private int pointerPressY;
    private int slideSpeed;
    private int listSelectedBottom;
    //-----------------------------------------------------------------------
    private String gameName;
    //------------------------------------------------------------------------
    private Image topBarImage;
    private Image bottomBarImage;
    private Image roomIconImage;
    private Image rowSelectedBackground;
    private Image rowTransparentBackground;
    private Image fullImage;
    private int maxNumberPlayer;
    private int countSlide;

    public ChooseRoomScene(MainCanvas parent) {
        super(parent);
        initValue();
        initObject();
    }

    private void initValue() {
        isDrawSelected = true;
        gamePlayType = gameData.getGameTypePlay();
        gameName = getGameName();
        maxNumberPlayer = 4 * ChooseTableScene.NUMBER_TABLES;
    }

    private void initObject() {
        try {
            initImage();
        } catch (IOException ex) {
        }
    }

    private void initImage() throws IOException {
        topBarImage = library.getTopBarImage();
        bottomBarImage = library.getBottomBarImage();
        roomIconImage = Utils.loadImage("room_icon.png");
        rowTransparentBackground = Utils.createTransparentImage(Constants.OPACITY_BLACK, canvasWidth, ROW_ITEM_HEIGHT);
        rowSelectedBackground = Utils.createTransparentImage(0xb2000000, canvasWidth, ROW_ITEM_HEIGHT);
        fullImage = Utils.loadImage("full6.png");
    }

    public void setListSubMenu() {
        subMenu.setList(new String[]{"Thông tin cá nhân", "Hộp thư", "Loa"});
    }

    public void doLoop() {
        super.doLoop();
        if (slideSpeed != 0 && countFrame % 2 == 0) {
            startDrawIndex += slideSpeed;
            countSlide++;
            if (countSlide == 2) {
                countSlide = 0;
                slideSpeed = 0;

            }
            parent.repaint();
        }
    }
//-------------------------------------------------------------------------

    public void paintComponents(Graphics g) {
        paintRoomList(g);
        paintBarMenu(g);

    }

    private void paintRoomList(Graphics g) {
        int topListRoom = topBarImage.getHeight() + 10;
        int leftListRoom = 10;
        if (startDrawIndex < 0) {
            startDrawIndex = 0;
        }
        for (int i = startDrawIndex; i < NUMBER_TOTAL_ROOMS; i++) {
            if (i == 0) {
                PiPoDesigner.drawString(g, "Khu tập sự", PiPoDesigner.COLOR_BLACK,
                        PiPoDesigner.COLOR_WHITE, leftScreen + leftListRoom, topScreen + topListRoom);
                topListRoom += 20;
                g.setColor(0xd8a600);
                g.fillRect(leftScreen, topScreen + topListRoom, canvasWidth, 1);

            }

            if (i == NUMBER_LOW_LEVEL_ROOMS) {
                topListRoom += 10;
                PiPoDesigner.drawString(g, "Khu nghiệp dư", PiPoDesigner.COLOR_BLACK,
                        PiPoDesigner.COLOR_WHITE, leftScreen + leftListRoom, topScreen + topListRoom);
                topListRoom += 20;
                g.setColor(0xd8a600);
                g.fillRect(leftScreen, topScreen + topListRoom, canvasWidth, 1);
            }

            if (i == NUMBER_MEDIUM_LEVEL_ROOMS + NUMBER_LOW_LEVEL_ROOMS) {
                topListRoom += 10;
                PiPoDesigner.drawString(g, "Khu chuyên nghiệp", PiPoDesigner.COLOR_BLACK,
                        PiPoDesigner.COLOR_WHITE, leftScreen + leftListRoom, topScreen + topListRoom);
                topListRoom += 20;
                g.setColor(0xd8a600);
                g.fillRect(leftScreen, topScreen + topListRoom, canvasWidth, 1);
            }
            if (i == roomSelectedIndex && isDrawSelected) {
                g.drawImage(rowSelectedBackground, leftScreen, topScreen + topListRoom + 1, 0);
            } else {
                g.drawImage(rowTransparentBackground, leftScreen, topScreen + topListRoom + 1, 0);
            }
            topListRoom += 10;
            g.drawImage(roomIconImage, leftListRoom, topScreen + topListRoom, 0);
            PiPoDesigner.drawString(g, "Phòng " + (i + 1),
                    PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, leftScreen + 50, topScreen + topListRoom + 5);
            PiPoDesigner.drawString(g, gameData.gaData[i] + "-" + maxNumberPlayer, PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE,
                    canvasWidth - 45, topScreen + topListRoom + 20);
            Utils.drawFrame(g, fullImage, canvasWidth - 40, topScreen + topListRoom + 5, getRoomLevel(gameData.gaData[i]), 0, fullImage.getWidth() / 6, fullImage.getHeight());
            topListRoom += 40;
            g.setColor(0xd8a600);
            g.fillRect(leftScreen, topScreen + topListRoom, canvasWidth, 1);
            if (i == roomSelectedIndex && isDrawSelected) {
                g.setColor(0xffffff);
                //g.drawRoundRect(leftScreen, topScreen + topListRoom - ROW_HEIGHT, canvasWidth - 1, ROW_HEIGHT, 5, 5);
                g.fillRect(leftScreen, topScreen + topListRoom - ROW_ITEM_HEIGHT, canvasWidth - 1, 1);
                g.fillRect(leftScreen, topScreen + topListRoom - ROW_ITEM_HEIGHT + ROW_ITEM_HEIGHT, canvasWidth - 1, 1);
                listSelectedTop = topListRoom - ROW_ITEM_HEIGHT;
                listSelectedBottom = topListRoom + ROW_ITEM_HEIGHT;
            }
        }
    }

    private int getRoomLevel(int number) {
        if (number == 0) {
            return 0;
        }
        if (0 <= number && number < 32) {
            return 1;
        }
        if (32 <= number && number < 64) {
            return 2;
        }
        if (64 <= number && number < 95) {
            return 3;
        }
        if (96 <= number && number < 127) {
            return 4;
        }
        if (128 <= number && number < 159) {
            return 4;
        }
        return 0;
    }

    private String getGameName() {
        switch (gamePlayType) {
            case Constants.CO_VUA:
                return "Cờ vua";
            case Constants.CUA_HANG:
            case Constants.PHOM:
                return "Phỏm";
            case Constants.BA_CAY:
                return "Ba cay";
            case Constants.TIEN_LEN:
                return "Tiến lên";
            case Constants.XENG:
                return "Xèng";

        }
        return "";
    }

    private void paintGameName(Graphics g) {
        PiPoDesigner.drawString(g, gameName, PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_YELOW, canvasWidth / 2 - PiPoDesigner.stringWidth(gameName) / 2,
                5);
    }

    public void paintBarMenu(Graphics g) {
        g.drawImage(topBarImage, leftScreen, 0, 0);
        g.drawImage(bottomBarImage, leftScreen, canvasHeight - bottomBarImage.getHeight(), 0);
        paintGameName(g);
        if (isShowSpeaker) {
            paintBarTextBox(g);
            return;
        }
        PiPoDesigner.drawString(g, "Menu", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, leftScreen + 5, canvasHeight - 25);
        PiPoDesigner.drawString(g, "Vào", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, canvasWidth / 2 - 15, canvasHeight - 25);
        PiPoDesigner.drawString(g, "Thoát", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, canvasWidth - 40, canvasHeight - 25);

    }
    //---------------------------------------------------------------------------

    protected void doKeyUpRemain() {

        Debug.d("key up");
        roomSelectedIndex--;
        Debug.d("room select :" + roomSelectedIndex);
        Debug.d("top list :" + listSelectedTop);
        if (listSelectedTop - ROW_ITEM_HEIGHT <= topBarImage.getHeight() && roomSelectedIndex != -1) {
            startDrawIndex--;
        }
        if (roomSelectedIndex < 0) {
            roomSelectedIndex = 0;
        }
        parent.repaint();

    }

    protected void doKeyDownRemain() {
        Debug.d("key down");
        roomSelectedIndex++;
        Debug.d("room select :" + roomSelectedIndex);
        Debug.d("top list :" + listSelectedTop);
        if (listSelectedBottom + ROW_ITEM_HEIGHT > canvasHeight - bottomBarImage.getHeight()) {
            startDrawIndex++;
        }
        if (roomSelectedIndex == NUMBER_TOTAL_ROOMS) {
            roomSelectedIndex = 0;
            startDrawIndex = 0;
        }
        parent.repaint();
    }

    protected void doSoftKeyRightRemain() {
        Request.exitChooseRoom();
        isLoading = true;
        parent.repaint();
    }

    protected void doKeyCenterRemain() {
        Request.enterRoom(roomSelectedIndex);
        isLoading = true;
        parent.repaint();
    }

    public void inputKeyCode(int key) {
        if (isShowSpeaker) {
            inputSpeaker(key);
            return;
        }
    }

    protected void touchPressedRemain(int x, int y) {
        touchSlide(x, y);
    }

    public void doTouchReleased(int x, int y) {
        Debug.d("pointer release");
        if (enableSlide) {
            touchSelectRoom(x, y);
        }
    }

    public void doTouchDraged(int x, int y) {
        if (y - pointerPressY < -10 && enableSlide) {
            Debug.d("start draw index <0:" + startDrawIndex);
            Debug.d("drag y:" + y);
            //   Debug.d("press y:" + pointerPressY );
            enableSlide = false;
            if (startDrawIndex <= NUMBER_TOTAL_ROOMS - 4) {
                slideSpeed = 1;
            }
            return;
        }
        if (y - pointerPressY > 10 && enableSlide) {
            Debug.d("start draw index >0:" + startDrawIndex);
            Debug.d("drag y:" + y);
            // Debug.d("press y:" + pointerPressY );
            enableSlide = false;
            if (startDrawIndex >= 0) {
                slideSpeed = -1;
            }
        }
    }

    private void touchSlide(int x, int y) {
        enableSlide = true;
        isDrawSelected = false;
        Debug.d("pressed x:" + x + ",y:" + y);
        pointerPressX = x;
        pointerPressY = y;
    }

    private void touchSelectRoom(int touchX, int touchY) {
        int topListRoom = topBarImage.getHeight() + 10;
//        int leftListRoom = 10;
        if (startDrawIndex < 0) {
            startDrawIndex = 0;
        }
        for (int i = startDrawIndex; i < NUMBER_TOTAL_ROOMS; i++) {
            if (i == 0) {
                topListRoom += 20;
            }

            if (i == NUMBER_LOW_LEVEL_ROOMS || i == NUMBER_MEDIUM_LEVEL_ROOMS + NUMBER_LOW_LEVEL_ROOMS) {
                topListRoom += 30;
            }
            topListRoom += 50;
            if (Utils.isTouch(leftScreen, topScreen + topListRoom - ROW_ITEM_HEIGHT, canvasWidth - 1, ROW_ITEM_HEIGHT, touchX, touchY)) {
                roomSelectedIndex = i;
                Debug.d("selected :" + roomSelectedIndex);
                doKeyCenter();
                break;
            }

        }
    }

    //--------------------------------------------------
    public void processMessage(String[] part) {
        super.processMessage(part);
        Debug.d("process choose room request:" + part[0]);
        if (part[0].equals("SM")) {
            gameData.setTableMoneyBet(part);
            return;
        }
        if (part[0].equals("ISPLAY")) {
            return;
        }
        if (part[0].equals("SE")) {
            gameData.setNumberPlayerInTable(part);
            gameData.setRoomId(roomSelectedIndex);
            showChooseTable();
            parent.repaint();
            return;
        }
        if (part[0].equals("RGA")) {
            isGoBack = true;
            showChooseGame();
            return;
        }
        if (part[0].equals("PASS")) {
            gameData.setTablePass(part);
            return;
        }
        if (part[0].equals("US")) {
            gameData.setMaxPlayerInTable(part);
            return;
        }
        if (part[0].equals("FULL")) {
            dialogString = "Phòng đã đủ người chơi";
            isLoading = false;
            parent.repaint();
            return;
        }
        if (part[0].equals("NSE")) {
            dialogString = "Bạn cần " + part[1] + " xu để vào phòng này.";
            isLoading = false;
            parent.repaint();
            return;
        }
    }

    public void dispose() {
        Debug.d("choose room dispose");
        gameName = null;
        topBarImage = null;
        bottomBarImage = null;
        roomIconImage = null;
        rowSelectedBackground = null;
        rowTransparentBackground = null;
        gameData.disposeGa();
        super.dispose();
    }

    protected void doSubMenu() {
        if (isShowMessage) {
            doMenuMessage();
            return;
        }
        isShowSubMenu = false;
        parent.repaint();
        switch (subMenu.getSelectedIndex()) {
            case 0:
                getMyinfo();
                break;
            case 1:
                getMyMessage();
                break;
            case 2:
                showSpeaker();
                break;
        }
    }

    protected void reponseQuestion(int result) {
    }

    public void doKeyPound() {
    }

    protected void doSoftKeyLeftRemain() {
    }

    public void doKeyLeft() {
        if (isShowMessage) {
            return;
        }
        if (isShowUserInfo) {
            moveLeftInfo();
            parent.repaint();
            return;
        }
    }

    public void doKeyRight() {
        if (isShowMessage) {
            return;
        }
        if (isShowUserInfo) {
            moveRightInfo();
            parent.repaint();
            return;
        }
    }
}
